package com.hhkj.study.admin.article.service.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.article.entity.Article;
import com.hhkj.study.admin.article.model.podo.ArticleDo;
import com.hhkj.study.admin.article.model.podo.ArticlePo;
import com.hhkj.study.admin.article.model.req.ArticleReq;
import com.hhkj.study.admin.article.model.res.ArticleRes;
import com.hhkj.study.admin.article.service.base.ArticleService;
import com.hhkj.study.admin.article.service.biz.ArticleBizService;
import com.hhkj.study.admin.article.service.extend.ArticleExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.hhkj.study.common.handler.CommonHandler;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* ArticleBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ArticleBizServiceImpl extends BaseBizServiceImpl<Article, ArticleReq, ArticleRes, ArticleDo, ArticlePo, ArticleService, ArticleExtendService> implements ArticleBizService {

    @Resource
    private GlobalCache globalCache;

    @Override
    public boolean updatePublishFlag(ArticleReq req) {
        Article article = this.getExtendService().getByIdNotNull(req.getId());
        if (article.getPublishedFlag().equals(req.getPublishedFlag())) {
            throw new GlobalException(SysConst.Flag.IS.getFlag().equals(req.getPublishedFlag()) ?
                    "已上架" : "已下架");
        }
        LambdaUpdateWrapper<Article> articleUpdate = new LambdaUpdateWrapper<>();
        articleUpdate.eq(Article::getId, req.getId())
                .set(Article::getPublishedFlag, req.getPublishedFlag()).set(Article::getPublishTime, new Date());
        return this.getService().update(articleUpdate);
    }

    @Override
    protected void beforeUpdate(ArticleReq articleReq) {
        checkExamStatus(articleReq);
    }

    @Override
    protected void beforeSave(ArticleReq articleReq) {
        checkExamStatus(articleReq);
    }


    @Override
    public boolean markArticle(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        LambdaQueryWrapper<Article> articleQuery = new LambdaQueryWrapper<>();
        articleQuery.in(Article::getId, idList).select(Article::getId, Article::getMainArticleFlag);
        List<Article> articleList = this.getService().list(articleQuery);
        if (CollectionUtil.isEmpty(articleList)) {
            return false;
        }
        articleList.forEach(article -> {
            Integer mainArticleFlag = SysConst.Flag.IS.getFlag().equals(article.getMainArticleFlag()) ? SysConst.Flag.NO.getFlag() : SysConst.Flag.IS.getFlag();
            article.setMainArticleFlag(mainArticleFlag);
        });
        return this.getService().updateBatchById(articleList);
    }

    @Override
    protected void afterGetById(ArticleRes articleRes) {
        suppleArticle(Lists.newArrayList(articleRes));
    }

    @Override
    protected void afterPage(List<ArticleRes> articleResList) {
        suppleArticle(articleResList);
    }

    /**
     * 校验考试动态是否合法
     * @param req
     */
    public void checkExamStatus(ArticleReq req) {
        // 选择了关联考试动态 考试动态标识不能为0
        if (ObjectUtil.isNotNull(req.getExamStatusId()) && !Objects.equals(req.getExamStatusFlag(), SysConst.Flag.IS.getFlag())) {
            throw new GlobalException("关联了考试动态,考试动态标识只能选择是");
        }
    }

    private void suppleArticle(List<ArticleRes> articleResList) {
        List<String> dictCodeList = Lists.newArrayList(DictCodeEnum.EXAM_TREND.getCode());
        dictCodeList.addAll(Constants.REGION_COURSE_CODE_LIST);
        Map<Long, String> dictMap = globalCache.cacheDictMap(dictCodeList);
        articleResList.forEach(articleRes -> {
            articleRes.setCourseTypeMap(CommonHandler.getMapByDict(dictMap, articleRes.getCourseTypeList()));
            articleRes.setCourseTypeName(CommonHandler.joinStr(dictMap, articleRes.getCourseTypeList(), Constants.VERTICAL_LINE));
            articleRes.setProvinceMap(CommonHandler.getMapByDict(dictMap, articleRes.getProvinceIdList()));
            articleRes.setProvinceName(CommonHandler.joinStr(dictMap, articleRes.getProvinceIdList(), Constants.VERTICAL_LINE));
            articleRes.setExamStatusName(dictMap.get(articleRes.getExamStatusId()));
        });
    }
}